package com.kepler.social.domain.support.media.mq;

import com.google.common.collect.Lists;
import com.kepler.social.common.Wrapper;
import com.kepler.social.common.utils.FileUtil;
import com.kepler.social.common.utils.Safes;
import com.kepler.social.domain.support.media.MediaDomainService;
import com.kepler.social.domain.support.media.converter.MediaConverter;
import com.kepler.social.domain.support.media.vo.CallBackVo;
import com.kepler.social.domain.support.media.vo.FileSliceMergeRsVo;
import com.kepler.social.domain.support.media.vo.FileSliceVo;
import com.kepler.social.domain.support.media.vo.M3u8Vo;
import com.kepler.social.generic.SpringContextHelper;
import com.lmax.disruptor.WorkHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@RequiredArgsConstructor
public class SliceMergeConsumer implements WorkHandler<SliceMergeMessage> {

    @Override
    public void onEvent(SliceMergeMessage message) {
        MediaDomainService mediaDomainService = SpringContextHelper.getBean(MediaDomainService.class);
        List<FileSliceMergeRsVo> mergeRsList = Lists.newArrayList();
        for (FileSliceVo fileSlice : message.getFileSlices()) {
            try {
                if (FileUtil.isImage(fileSlice.getMimeType())) {
                    String url = mediaDomainService.mergeImageSlice(fileSlice.getSeqNo(), fileSlice.getFileName());
                    mergeRsList.add(MediaConverter.toImageSliceMergeRsVo(fileSlice, url));
                } else if (FileUtil.isVideo(fileSlice.getMimeType())) {
                    M3u8Vo m3u8Vo = mediaDomainService.mergeVideoSlice(fileSlice.getSeqNo(), fileSlice.getFileName());
                    mergeRsList.add(MediaConverter.toVideoSliceMergeRsVo(fileSlice, m3u8Vo));
                } else {
                    throw new IllegalArgumentException("非视频和文件");
                }
            } catch (IOException e) {
                log.error("slice merge seq {} fileName {} err {}", fileSlice.getSeqNo(), fileSlice.getFileName(), e, e);
                mergeRsList.add(MediaConverter.toFailureSliceMergeRsVo(fileSlice, e.getMessage()));
                break;
            } catch (IllegalArgumentException e) {
                mergeRsList.add(MediaConverter.toFailureSliceMergeRsVo(fileSlice, e.getMessage()));
                break;
            }
        }

        CallBackVo callBack = message.getCallBack();
        // 将结果传递回去
        try {
            SpringContextHelper.invokeMethod(callBack.getClassName(), callBack.getClassMethodName(),
                    new Object[]{callBack.getBusinessKey(), new Wrapper(mergeRsList)});
        } catch (Exception ex) {
            log.error("sliceMerge callback  seq {} err: {}",
                    Safes.of(mergeRsList).stream().map(e -> e.getSeqNo()).collect(Collectors.joining(",")),
                    ex, ex);
        }
        log.info("sliceMerge consumer res: {}", Safes.of(mergeRsList).stream().map(e -> e.getSeqNo())
                .collect(Collectors.joining(",")));
    }

}